package cn.com.git.example.module.service.impl;

import cn.com.git.example.common.ResultConstant;
import cn.com.git.example.common.ResultPage;
import cn.com.git.example.common.util.object.MapperFacadeUtils;
import cn.com.git.example.module.dao.mapper.SysUserMapper;
import cn.com.git.example.module.dao.model.SysUser;
import cn.com.git.example.module.service.SysUserService;
import cn.com.git.example.module.service.dto.SysUserDTO;
import cn.com.git.example.module.service.query.SysUserListQuery;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zhangzhenfu
 * @Description
 * @date 2021/3/6 0006 14:58
 */
@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public ResultPage<SysUserDTO> pageSysUser(SysUserListQuery query) {
        List<SysUserDTO> data = new ArrayList<>();
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
        List<SysUser> list = sysUserMapper.selectListByQuery(query);
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .stream().forEach(item -> {
            SysUserDTO d = MapperFacadeUtils.map(item,SysUserDTO.class);
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS, data, data.size());
    }

    @Override
    public List<SysUserDTO> listSysUser(SysUserListQuery query) {
        List<SysUserDTO> data = new ArrayList<>();
        List<SysUser> list = sysUserMapper.selectListByQuery(query);
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .stream().forEach(item -> {
            SysUserDTO d = MapperFacadeUtils.map(item,SysUserDTO.class);
            data.add(d);
        });
        return data;
    }

    @Override
    public SysUserDTO getSysUser(Integer id) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(id);
        SysUserDTO sysUserDTO = MapperFacadeUtils.map(sysUser,SysUserDTO.class);
        return sysUserDTO;
    }
}
